package cn.gok.manage.service.impl;

import cn.gok.dao.CartMapper;
import cn.gok.dao.OrderMapper;
import cn.gok.dao.ProductMapper;
import cn.gok.manage.service.ICartService;
import cn.gok.manage.web.vo.CartListVO;
import cn.gok.manage.web.vo.CartVO;
import cn.gok.pojo.Cart;
import cn.gok.pojo.Order;
import cn.gok.pojo.Product;
import cn.gok.util.Generator13;
import cn.gok.util.ResponseCode;
import cn.gok.util.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author: 鸭鸭
 * @ClassName:CartServiceImpl
 * @Package:cn.gok.manage.service.impl
 * @Description:
 * @date: 2021/8/6
 * @time: 10:00
 */
@Service("cartService")
public class CartServiceImpl implements ICartService {
    @Autowired
    CartMapper cartMapper;
    @Autowired
    ProductMapper productMapper;

    @Override
    public ResultData selectAllCartByUserId(int user_id) {
        if(user_id==0){
            return ResultData.fail_login("用户未登录,请登录");
        }
        List<Cart> carts = cartMapper.selectCartByUserId(user_id);
        List<CartListVO> cartListVOs = new ArrayList<>();
        BigDecimal cartTotalPrice=BigDecimal.valueOf(0);

        int AllCheckFlag = 0;
        for(Cart c : carts){
            CartListVO cartListVO = new CartListVO();
            int product_id = c.getProductId();
            Product product = productMapper.queryProductById(product_id);
            cartListVO.setId(c.getId());
            cartListVO.setUser_id(c.getUserId());
            cartListVO.setProduct_id(c.getProductId());
            cartListVO.setQuantity(c.getQuantity());
            cartListVO.setProductName(product.getName());
            cartListVO.setProductSubtitle(product.getSubtitle());
            cartListVO.setProductMainImage(product.getMainImage());
            cartListVO.setProductPrice(product.getPrice());
            cartListVO.setProductStatus(product.getStatus());
            //计算总价
            BigDecimal quantity = BigDecimal.valueOf((int)c.getQuantity());
            BigDecimal price = product.getPrice();
            BigDecimal totalPrice = quantity.multiply(price);
            //计算购物车总价
            if(c.getChecked()==1){
                cartTotalPrice = cartTotalPrice.add(totalPrice);
            }
            cartListVO.setProductTotalPrice(totalPrice);
            cartListVO.setProductStock(product.getStock());
            cartListVO.setProductChecked(c.getChecked());
            if(c.getChecked() == 0){
                AllCheckFlag=1;
            }

            //判断是否超出库存
            if(c.getQuantity() <= product.getStatus() ) {
                cartListVO.setLimitQuantity(ResponseCode.LIMIT_NUM_SUCCESS.getMsg());
            }else{
                cartListVO.setLimitQuantity(ResponseCode.LIMIT_NUM_FAIL.getMsg());
            }
            cartListVOs.add(cartListVO);
        }
        CartVO cartVO = new CartVO();
        cartVO.setCartListVOs(cartListVOs);
        cartVO.setCartTotalPrice(cartTotalPrice);
        if(AllCheckFlag==0){
            cartVO.setAllChecked(true);
        }else{
            cartVO.setAllChecked(false);
        }
        return ResultData.createData(cartVO);
    }

    /*
     *登录后添加商品到购物车
     */
    @Override
    public ResultData addCart(int user_id,int product_id,int count) {
        System.out.println("Service中的user_id"+user_id);
        Cart cart = cartMapper.selectCartByUserIdAndProductId(user_id,product_id);
        System.out.println("cart"+cart);
        if(cart==null){
            int flag =0;
            Cart c = new Cart();
            c.setUserId(user_id);
            c.setProductId(product_id);
            c.setQuantity(count);
            c.setChecked(1);
            c.setCreateTime(new Date());
            c.setUpdateTime(new Date());
            flag=cartMapper.insertSelective(c);
            if(flag == 0){
                return ResultData.fail_login("用户未登录,请登录");
            }else{
                return selectAllCartByUserId(cart.getUserId());
            }
        }else{
            int total = count+cart.getQuantity();
            Cart c2 = new Cart();
            c2.setUserId(user_id);
            c2.setProductId(product_id);
            c2.setUpdateTime(new Date());
            c2.setQuantity(total);
            cartMapper.updateCartByProductId(c2);
            return selectAllCartByUserId(cart.getUserId());
        }
    }

    @Override
    public ResultData updateCartCountByProductId(int user_id,int product_id,int count) {
        Cart cart = cartMapper.selectCartByUserIdAndProductId(user_id,product_id);
        if(cart==null){
            return ResultData.fail_login("用户未登录,请登录");
        }
        Cart c = new Cart();
        int total = cart.getQuantity() + count;
        c.setUserId(user_id);
        c.setProductId(product_id);
        c.setQuantity(total);
        c.setUpdateTime(new Date());
        cartMapper.updateCartByProductId(c);
        return selectAllCartByUserId(cart.getUserId());
    }

    @Override
    public ResultData deleteCarts(List<Integer> list, int userId) {
        if(userId+""==null){
            return ResultData.fail("用户未登录，请登录");
        }
        int flag = cartMapper.deleteCartByProductIds(list,userId);
        if(flag==0){
            return ResultData.fail_login("出现异常，购物车中没有商品");
        }
        return selectAllCartByUserId(userId);
    }

    @Override
    public ResultData updateCheckedByProductId(int userId,int productId) {
        if(userId+""==null){
            return ResultData.fail("用户未登录，请登录");
        }
        Cart cart = new Cart();
        cart.setUserId(userId);
        cart.setProductId(productId);
        cart.setChecked(1);
        cart.setUpdateTime(new Date());

        int flag = cartMapper.updateByUserIdProductIdSelective(cart);
        if(flag==0){
            return ResultData.fail("出现异常，购物车中没有商品");
        }
        return selectAllCartByUserId(userId);
    }

    @Override
    public ResultData updateNoCheackedByProductId(int userId, int productId) {
        if(userId+""==null){
            return ResultData.fail("出现异常,用户未登录");
        }
        Cart cart = new Cart();
        cart.setUserId(userId);
        cart.setProductId(productId);
        cart.setChecked(0);
        cart.setUpdateTime(new Date());
        int flag = cartMapper.updateByUserIdProductIdSelective(cart);
        if(flag==0){
            return ResultData.fail("出现异常，购物车中没有商品");
        }
        return selectAllCartByUserId(userId);
    }

    @Override
    public ResultData selectCountByUserId(int userId) {
        List<Cart> carts = cartMapper.selectCartByUserId(userId);
        if(userId+""==null){
            return ResultData.fail("出现异常,用户未登录");
        }
        if(carts==null){
            return ResultData.createData(0);
        }
        int count = 0;
        for(Cart c:carts){
            count+=c.getQuantity();
        }
        return ResultData.createData(count);
    }

    @Override
    public ResultData updateAllCheckedByUserId(int userId) {
        List<Cart> carts = cartMapper.selectCartByUserId(userId);
        if(userId+"" == null){
            return ResultData.fail("用户未登录,请登录");
        }
        if(carts==null){
            return ResultData.fail("出现异常，购物车没有商品");
        }
        for(Cart c:carts){
            Cart c2 = new Cart();
            c2.setUserId(userId);
            c2.setProductId(c.getProductId());
            c2.setUpdateTime(new Date());
            c2.setChecked(1);
            cartMapper.updateByUserIdProductIdSelective(c2);
        }
        return selectAllCartByUserId(userId);
    }

    @Override
    public ResultData updateAllUnCheckedByUserId(int userId) {
        List<Cart> carts = cartMapper.selectCartByUserId(userId);
        if(userId+"" == null){
            return ResultData.fail("用户未登录,请登录");
        }
        if(carts==null){
            return ResultData.fail("出现异常，购物车没有商品");
        }
        for(Cart c:carts){
            Cart c2 = new Cart();
            c2.setUserId(userId);
            c2.setProductId(c.getProductId());
            c2.setUpdateTime(new Date());
            c2.setChecked(0);
            cartMapper.updateByUserIdProductIdSelective(c2);
        }
        return selectAllCartByUserId(userId);
    }



}
